package com.hiof.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.hiof.Configuration;
import com.hiof.R;
import com.hiof.servercommunication.RestFulServiceManager;
import com.hiof.servercommunication.exception.NetworkException;
import com.hiof.servercommunication.exception.ServerException;
import com.hiof.servercommunication.status.LoginStatus;
import com.hiof.settings.SettingManager;
import com.hiof.settings.user.UserConfiguration;
import com.hiof.utility.Utility;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 3/22/12
 * Time: 10:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoginActivity extends Activity implements View.OnClickListener, Runnable {
    private Button btnLogin;
    private Button btnCancel;
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox chkboxAutoLogin;
    private ProgressDialog userLoginProgressDialog;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        Configuration.getInstance().setCurrentContext(this);
        btnLogin = (Button) findViewById(R.id.login_button);
        btnLogin.setOnClickListener(this);
        btnCancel = (Button) findViewById(R.id.cancel_button);
        btnCancel.setOnClickListener(this);
        etUsername = (EditText) findViewById(R.id.login_txtusername);
        etPassword = (EditText) findViewById(R.id.login_txtpassword);
        chkboxAutoLogin = (CheckBox) findViewById(R.id.login_chkboxAutoLogin);
    }

    @Override
    public void onClick(View view) {
        if(view == btnLogin){
            userLoginProgressDialog = Utility.getProgressBar(getResources().getString(R.string.progress_bar_sign_in_title), this);
            userLoginProgressDialog.show();
            SettingManager.getInstance().setAutoLogin(chkboxAutoLogin.isChecked());
            Thread loginThread = new Thread(this);
            loginThread.start();
        } else if(view==btnCancel){
            finish();
        }
    }

    void logIn(String userName, String password){
        Message msg = new Message();
        Bundle bundle = new Bundle();
        msg.setData(bundle);

        LoginStatus loginStatus = LoginStatus.UserNotExist;
        try {
            loginStatus = RestFulServiceManager.getInstance().logIn(userName, password);
        } catch (NetworkException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ServerException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if(loginStatus== LoginStatus.OK){
//            saving in shared preference
            SettingManager.getInstance().setUsername(userName);
            SettingManager.getInstance().setPassword(password);
//            saving in memory in singleton userconfiguration class
//            UserConfiguration.getInstance().setUserName(userName);
            bundle.putString("message","user logged in");

        } else {
            bundle.putString("message","user not logged in");
        }
        loginHandler.sendMessage(msg);
    }
    Handler loginHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            userLoginProgressDialog.dismiss();
            if(msg.getData().get("message").equals("user logged in"))  {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RaceListActivity.class);
                startActivity(intent);
                finish();
            }   else{
                Toast.makeText(LoginActivity.this, "User name or password incorrect", Toast.LENGTH_LONG);
            }
        }
    };

    @Override
    public void run() {
        String userName = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        logIn(userName,password);
    }
}